<?php
/**
 * Created by PhpStorm.
 * User: 牛赞赞
 * Date: 2021/1/20
 * Time: 16:01
 */

namespace App\Task;


use App\Game\Core\Packet;
use App\Game\Conf\MainCmd;
use App\Game\Conf\SubCmd;
use Psr\Container\ContainerInterface;
use Swoole\WebSocket\Server as WebSocketServer;

class CallNextTask
{
    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    /**
     * @var ContainerInterface
     */
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function callNext($room_no,$next_chair_id,$call_catch,$times,$call_timer):void {
        $game_conf = config('game');
        $serv = server();
        $fds  = $this->getOtherFdsByRoomNo($room_no);
        $room_data_key = sprintf($game_conf['user_room_data'],$room_no);
        $room_data = redis()->hGetAll($room_data_key);
        // 更新计时器序号
        $this->updateCallTimes($room_no,$call_timer);
        if(!isset($room_data['master'])){
            // 通知玩家叫牌
            $this->nextCaller($next_chair_id,$call_catch,$serv,$fds);

            // 投递计时器
            $task = container()->get(CallMasterTimerTask::class);
            $task->callMasterTimer($room_no,$next_chair_id,$call_catch,$times,$call_timer);
        }
    }

    /**
     * @param $room_no
     * @param $times
     */
    protected function updateCallTimes($room_no,$times){
        $game_conf = config('game');
        $user_room_callTimes = sprintf($game_conf['user_room_callTimes'],$room_no);
        redis()->set($user_room_callTimes, $times);
    }

    /**
     * 获取房间其他用户的fds
     * @param $room_no
     * @return array
     */
    protected function getOtherFdsByRoomNo($room_no){
        $game_conf = config('game');
        $room_wait_key = sprintf($game_conf['user_room_wait'],$room_no);
        $room_users = json_decode(redis()->get($room_wait_key),true);
        $fds = [];
        foreach ($room_users as $k=>$v){
            $bind_key = sprintf($game_conf['user_bind_key'],$v);
            $fds[] = redis()->get($bind_key);
        }
        return $fds;
    }

    /**
     * 增加叫牌次数
     * @param $room_no
     * @param $params
     */
    protected function updateRoomData($room_no,$params){
        $game_conf = config('game');
        $room_data_key = sprintf($game_conf['user_room_data'],$room_no);
        foreach ($params as $key=>$val){
            redis()->hSet($room_data_key, (string)$key, $val);
        }
    }

    /**
     * 房间广播下次叫牌玩家
     * @param $chair_id
     * @param $call_catch
     * @param $serv
     * @param $fds
     */
    protected function nextCaller($chair_id,$call_catch,$serv,$fds){
        $user_conf = config('user');
        $msg = ['status'=>'继续叫地主','chair_id'=>$chair_id,'call_or_catch'=>$call_catch,'wait_times'=>$user_conf['call_timer']];
        $data = Packet::packFormat('OK', 1, $msg);
        $data = Packet::packEncode($data, MainCmd::CMD_SYS, SubCmd::SUB_GAME_CALL_LOOP_RESP);
        foreach($fds as $fd) {
            //获取客户端信息
            $client_info = $serv->getClientInfo($fd);
            $client[$fd] = $client_info;
            if(isset($client_info['websocket_status']) && $client_info['websocket_status'] == 3) {
                $serv->push($fd, $data, WEBSOCKET_OPCODE_BINARY);
            }
        }
    }
}